<!-- 新建 -->
<template>
  <div id='newbuild'>
    <div class="newbuild"
         @click="handleNewBuild">
      <span>
        <img :src="MY_URL[mesObj.type]"
             alt="">
      </span>
      <div v-if="isNewBuild"
           class="input-box">
        <el-input v-model="name"
                  ref="myInput"
                  placeholder="请输入内容"
                  @blur="handleBlur"
                  @keyup.enter.native="$event.target.blur">
        </el-input>
        <i class="el-icon-right"
           @click="handleClickAdd"></i>
      </div>

      <p v-else>{{mesObj.name}}</p>
    </div>
  </div>

</template>

<script>

import {
  MY_URL,
  regSpecCharEn,
  regSpecCharCn,
} from '@/utils/constants';
import { _debounce, getPureFileName } from '@/utils/public';

export default {
  components: {},
  data() {
    return {
      MY_URL,
      isNewBuild: false,
      // 优化表单验证体验
      hasCheckingErr: false,
      name: '',
    };
  },
  props: {
    mesObj: {
      type: Object,
    },
    showList: {
      type: Array,
      default: () => [],
    },
  },
  computed: {},
  created() {
  },
  mounted() {
    this.name = this.mesObj.name;
  },
  watch: {
    isNewBuild: {
      handler(newVal) {
        if (newVal) {
          this.$nextTick(() => {
            this.$refs.myInput.focus();
            this.$refs.myInput.select();
          });
        }
      },
    },
  },
  methods: {
    handleNewBuild: _debounce(function () {
      this.isNewBuild = true;
    }),
    getSimpleFiles(files) {
      return files.map((item) => ({
        name: getPureFileName(item.name, !item.etag), type: item.type,
      }));
    },
    handleBlur: _debounce(function () {
      if (!this.hasCheckingErr) {
        this.isNewBuild = false;
        this.name = this.mesObj.name;
      } else {
        // 为了防止出现问题，验证未通过时要继续选中input
        this.$nextTick(() => {
          this.$refs.myInput.focus();
          this.$refs.myInput.select();
        });
      }
      this.hasCheckingErr = false;
    }),
    checkName() {
      if (!this.name) {
        this.$message.error('文件名不能为空');
        return false;
      }
      if (regSpecCharEn.test(this.name) || regSpecCharCn.test(this.name)) {
        this.$message.error('文件名不能包含特殊字符');
        return false;
      }
      const simpleFiles = this.getSimpleFiles(this.showList);
      const currentTypeFiles = simpleFiles.filter((item) => item.type === this.mesObj.type);
      if (currentTypeFiles.some((file) => file.name === this.name)) {
        this.$message.error('同一目录下的文件名不能重复');
        return false;
      }

      return true;
    },
    handleClickAdd() {
      if (this.checkName()) {
        this.$emit('change', { name: this.name, type: this.mesObj.type });
        this.name = this.mesObj.name;
      } else {
        this.hasCheckingErr = true;
      }
    },
  },
};
</script>
<style lang='scss' scoped>
#newbuild {
  width: 100%;
}
.newbuild {
  width: 100%;
  display: flex;
  align-items: center;
  cursor: pointer;
  & span {
    width: 20px;
    margin-right: 14px;
  }
  & img {
    max-width: 20px;
    height: 20px;
  }
  & p {
    font-size: 16px;
    color: #020221;
  }
  .input-box {
    display: flex;
    align-items: center;
    width: 170px;
    height: 32px;
    border: 1px solid #e4e8f1;
    border-radius: 8px;
    padding: 0 8px;
  }
}
.newbuild /deep/ .el-input__inner {
  padding: 0;
  height: 30px;
  border: none;
  background: transparent;
}

.el-icon-right {
  color: #51a8ff;
  font-size: 26px;
  cursor: pointer;
}
</style>
